import Vue from 'vue'
import Vuex from 'vuex'
import getters from './getters'
import app from './modules/app'
import settings from './modules/settings'
import user from './modules/user'

Vue.use(Vuex)

const req = require.context('./modules', false, /\.js$/)

// req 是一个函数  webpackContext(req){...}
// console.log('req=>', req)

// req.keys() => ['./app.js', './settings.js', './user.js']
// console.log('req.keys()=>', req.keys())

// req.keys().map(item => { return item.split('./') }))  log结果: 
// (3) [Array(2), Array(2), Array(2)]
// 0: (2) ['', 'app.js']
// 1: (2) ['', 'settings.js']
// 2: (2) ['', 'user.js']
// console.log(req.keys().map(item => { return item.split('./') }))

const mName = req.keys().map(item => { return item.split('./')[1] }).map(item => { return item.split('.js')[0] })
const mModule = req.keys().map(req)

const obj = {}

mName.forEach((key, index) => {
  // console.log('key', key);
  // console.log('index', index);
  obj[key] = mModule[index].default

});
// console.log('obj', obj);


const store = new Vuex.Store({
  modules: obj,
  getters
})

export default store
